import textwrap
from typing import Final, final
from django.template.defaultfilters import slugify
from server.apps.accounts.models import CustomUser
from django.db import models

#: That's how constants should be defined.
_POST_TITLE_MAX_LENGTH: Final = 80



@final
class Post(models.Model):
   
    title = models.CharField(max_length=_POST_TITLE_MAX_LENGTH)
    slug = models.SlugField(null=False, max_length=255, unique=True)
    body = models.TextField()
    author = models.ForeignKey(CustomUser, on_delete=models.PROTECT)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

   
    def __str__(self) -> str:
        return textwrap.wrap(self.title, _POST_TITLE_MAX_LENGTH // 4)[0]
    

    def save(self, *args, **kwargs):
        # if not self.slug:
        self.slug = slugify(self.title)
        return super().save(*args, **kwargs)
    
